/*
 * Copyright (C) 2011 CrystalRange
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.crystalrange.enterprise.web;

import com.googlecode.genericdao.search.Search;
import java.util.List;
import java.util.Set;
import org.crystalrange.BaseController;
import org.crystalrange.enterprise.model.CompanyCode;
import org.crystalrange.enterprise.service.CompanyCodeService;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Listitem;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Window;

public class CompanyCodeListController extends BaseController {

    protected CompanyCodeService companyCodeService;
    protected CompanyCode selectedCompanyCode;
    protected Listbox listboxCompanyCode;
    protected Search searchCompanyCode;
    protected List<CompanyCode> companyCodes;

    public CompanyCodeService getCompanyCodeService() {
        return this.companyCodeService;
    }

    public void setCompanyCodeService(CompanyCodeService companyCodeService) {
        this.companyCodeService = companyCodeService;
    }

    public CompanyCode getSelectedCompanyCode() {
        return this.selectedCompanyCode;
    }

    public void setSelectedCompanyCode(CompanyCode selectedCompanyCode) {
        this.selectedCompanyCode = selectedCompanyCode;
    }

    public List<CompanyCode> getCompanyCodes() {
        return this.companyCodes;
    }

    @Override
    public void doAfterCompose(Component component) throws Exception {
        super.doAfterCompose(component);

        this.searchCompanyCode = new Search(CompanyCode.class);
        this.searchCompanyCode.setMaxResults(100);
        this.searchCompanyCode.addFetch("createdBy");
        this.searchCompanyCode.addFetch("updatedBy");
        this.companyCodes = this.companyCodeService.search(this.searchCompanyCode);

        component.setAttribute("controller", this);
    }

    public void onClick$menuitemCompanyCodeCreate(Event event) throws Exception {
        this.companyCodeCreate();
    }

    public void onClick$menuitemCompanyCodeEdit(Event event) throws Exception {
        this.companyCodeEdit();
    }

    public void onClick$menuitemCompanyCodeDelete(Event event) throws Exception {
        this.companyCodeDelete();
    }

    public void onClick$menuitemCompanyCodeSearch(Event event) throws Exception {
        this.companyCodeSearch();
    }

    public void onClick$menuitemCompanyCodeRefresh(Event event) throws Exception {
        this.companyCodeRefresh();
    }

    public void onClick$toolbarbuttonCompanyCodeCreate(Event event) throws Exception {
        this.companyCodeCreate();
    }

    public void onClick$toolbarbuttonCompanyCodeEdit(Event event) throws Exception {
        this.companyCodeEdit();
    }

    public void onClick$toolbarbuttonCompanyCodeDelete(Event event) throws Exception {
        this.companyCodeDelete();
    }

    public void onClick$toolbarbuttonCompanyCodeSearch(Event event) throws Exception {
        this.companyCodeSearch();
    }

    public void onClick$toolbarbuttonCompanyCodeRefresh(Event event) throws Exception {
        this.companyCodeRefresh();
    }
    
    public void onDoubleClickListItem(Listitem listitem) throws Exception {
        this.companyCodeEdit();
    }
    
    private void companyCodeCreate() throws Exception {
        this.execution.sendRedirect("~./zul/html/companyCode.zul");
    }

    private void companyCodeEdit() throws Exception {
        if (this.selectedCompanyCode != null) {
            this.execution.sendRedirect("~./zul/html/companyCode.zul?Id=" + this.selectedCompanyCode.getId().toString());
        }
    }

    private void companyCodeDelete() throws Exception {
        Set<Listitem> selectedItems = this.listboxCompanyCode.getSelectedItems();

        if (selectedItems.size() > 0) {
            if (Messagebox.YES == Messagebox.show("Delete these records?", "Delete?", 
                    Messagebox.YES | Messagebox.NO, Messagebox.QUESTION)) {
                for (Listitem selectedItem : selectedItems) {
                    CompanyCode companyCode = (CompanyCode) selectedItem.getValue();
                    this.companyCodeService.remove(companyCode);
                }
            }
        } else {
            this.message.setText("Please select at least one item!");
            this.message.setIcon("/img/exclamation.png");
        }

        this.companyCodes = this.companyCodeService.search(this.searchCompanyCode);
        this.binder.loadAll();
    }

    private void companyCodeSearch() throws Exception {
        this.execution.setAttribute("searchCompanyCode", this.searchCompanyCode);

        Window window = (Window) this.execution.createComponents("~./zul/html/companyCodeSearch.zul", null, null);
        window.setParent(self);
        window.doModal();

        if (this.execution.getAttribute("result") == "ok") {
            this.searchCompanyCode = (Search) this.execution.getAttribute("searchCompanyCode");
        }

        this.companyCodes = this.companyCodeService.search(this.searchCompanyCode);
        this.binder.loadAll();
    }

    private void companyCodeRefresh() throws Exception {
        this.companyCodes = this.companyCodeService.search(this.searchCompanyCode);
        this.binder.loadAll();
    }
}
